﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
    public class ContentMetaDataMasterDao : IDaoInterface<ContentMetaDataMaster, ContentMetaDataMasterCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region IDaoInterface<ContentMetaDataMaster, ContentMetaDataMasterCriteria> Members
        public bool Insert(ContentMetaDataMaster ContentMetaDataMasterEntity)
        {
            bool IsOperationSuccessful = false;
            if (ContentMetaDataMasterEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                //Mrinmoy - have copied the below code to a new private method "TranslateContentMetaDataMasterFromEntityToDatabaseParameters()"
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentCode", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentCode);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentName", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentName);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaTagKeyword", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentMetaTagKeyword);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaTagDescription", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentMetaTagDescription);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentHtmlData", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentHtmlData);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTypeKey);
                ////Mrinmoy - When a new content metadata is created it will not have any statitcs.
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalDownloads", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalDownloads);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalLikes", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalLikes);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalViews", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalViews);

                TranslateContentMetaDataMasterFromEntityToDatabaseParameters(CmdObj, ContentMetaDataMasterEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContentMetaDataMasterEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);



                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {
                    ContentMetaDataMasterEntity.ContentKey = Convert.ToInt32(CmdObj.Parameters["@ContentKey"].Value);
                    ContentMetaDataMasterEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    ContentMetaDataMasterEntity.Sys_DateOfModification = ContentMetaDataMasterEntity.Sys_DateOfCreation;
                    ContentMetaDataMasterEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new ContentMetaMasterData entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(ContentMetaDataMaster ContentMetaDataMasterEntity)
        {
            bool IsOperationSuccessful = false;

            if (ContentMetaDataMasterEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                DataAccessProviderUtility.AddParameter(CmdObj, "@ContentKey", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentKey);

                //Mrinmoy - have copied the below code to a new private method "TranslateContentMetaDataMasterFromEntityToDatabaseParameters()"
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentCode", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentCode);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentName", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentName);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaTagKeyword", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentMetaTagKeyword);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaTagDescription", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentMetaTagDescription);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentHtmlData", ParameterDirection.Input, DbType.String, ContentMetaDataMasterEntity.ContentHtmlData);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTypeKey);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalDownloads", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalDownloads);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalLikes", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalLikes);
                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTotalViews", ParameterDirection.Input, DbType.Int32, ContentMetaDataMasterEntity.ContentTotalViews);

                TranslateContentMetaDataMasterFromEntityToDatabaseParameters(CmdObj, ContentMetaDataMasterEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ContentMetaDataMasterEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords == 1)
                {
                    ContentMetaDataMasterEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    ContentMetaDataMasterEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing ContentMetaMasterData entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(ContentMetaDataMaster ContentMetaDataMasterDataEntity)
        {
            bool IsOperationSuccessfull = false;

            ContentMetaDataMasterDataEntity.EntityState = EntityState.Changed;
            ContentMetaDataMasterDataEntity.Sys_IsDeleted = true;
            if (Update(ContentMetaDataMasterDataEntity))
            {
                IsOperationSuccessfull = true;
            }
            return IsOperationSuccessfull;
        }

        public ContentMetaDataMaster GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ContentMetaDataMaster> GetAll()
        {
            throw new NotImplementedException();
        }

        public List<ContentMetaDataMaster> GetByCriteria(ContentMetaDataMasterCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }
        #endregion Members

        public ContentMetaDataMaster GetByContentKey(int ContentKey)
        {
            ContentMetaDataMaster _ContentMetaDataMasterList = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_GetByContentKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ContentKey));


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _ContentMetaDataMasterList = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateContentMetaDataMasterFieldsFromReaderToEntity(_ContentMetaDataMasterList, _DataReader);
                }
            }
            else
            {
                throw new Exception("No ContentMeatDataMaster found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _ContentMetaDataMasterList;
        }

        public ContentMetaDataMaster GetByContentCode(string ContentCode)
        {
            //Mrinmoy -  the below statement in not declaration it is actually creating an instance. only declare the object no instance should be created by using NEW
            //Mrinmoy - Why _ContentMetaDataMasterList is named as a list when it it returning only one instance of entity?
            ContentMetaDataMaster _SeletedContentMetaDataMaster = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_GetByContentCode";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentCode", ParameterDirection.Input, DbType.String, ContentCode);


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SeletedContentMetaDataMaster = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateContentMetaDataMasterFieldsFromReaderToEntity(_SeletedContentMetaDataMaster, _DataReader);
                }
            }
            else
            {
                throw new Exception("No ContentMetaDataMaster found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _SeletedContentMetaDataMaster;
        }

        public ContentMetaDataMaster GetByContentName(string ContentName)
        {
            ContentMetaDataMaster _ContentMetaDataMasterList = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_GetByContentName";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentName", ParameterDirection.Input, DbType.String, ContentName);


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _ContentMetaDataMasterList = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateContentMetaDataMasterFieldsFromReaderToEntity(_ContentMetaDataMasterList, _DataReader);
                }
            }
            else
            {
                throw new Exception("No ContentMetaDataMaster found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _ContentMetaDataMasterList;
        }

        public List<ContentMetaDataMaster> GetByContentTypeKey(int ContentTypeKey)
        {
            List<ContentMetaDataMaster> _ContentMetaDataMasterList = new List<ContentMetaDataMaster>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentMetaDataMaster_GetAllByContentTypeKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, ContentTypeKey);

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    ContentMetaDataMaster _TempContentMetaDataMaster = new ContentMetaDataMaster(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateContentMetaDataMasterFieldsFromReaderToEntity(_TempContentMetaDataMaster, _DataReader);
                    _ContentMetaDataMasterList.Add(_TempContentMetaDataMaster);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _ContentMetaDataMasterList;
        }

        public List<ContentStatistics> GetAllContentStatisticsWithContentTypeKey(int ContentTypeKey)
        {
            List<ContentStatistics> _ContentStatisticsList = new List<ContentStatistics>();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentStatistics_GetAllConetentMetaDataMasterWithStatisticsByContentType";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, ContentTypeKey);

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();

            DbDataReader _DataReader = CmdObj.ExecuteReader();
            if (_DataReader.HasRows)
            {
                while (_DataReader.Read())
                {
                    ContentStatistics _TempContentStatistics = new ContentStatistics(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateContentStatisticsFieldsFromReaderToEntity(_TempContentStatistics, _DataReader);
                    _ContentStatisticsList.Add(_TempContentStatistics);
                }
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _ContentStatisticsList;
        }

        public ContentStatistics GetContentStatisticsWithContneKey(int ContentKey)
        {
            ContentStatistics _ContentStatisticsList = new ContentStatistics();

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentStatistics_GetContentMetaDataMasterWithContentStaticstics";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentKey", ParameterDirection.Input, DbType.Int32, ContentKey);

            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _ContentStatisticsList = new ContentStatistics(); //IMPORTANT NOTE:- Create instance only when required.
                    this.TranslateContentStatisticsFieldsFromReaderToEntity(_ContentStatisticsList, _DataReader);
                }
            }
            else
            {
                throw new Exception("No ContentMeatDataMaster found");
            }
            CmdObj.Dispose();
            DbConn.Close();

            return _ContentStatisticsList;
        }

        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateContentMetaDataMasterFieldsFromReaderToEntity(ContentMetaDataMaster pContentMetaDataMaster, DbDataReader pDbDataReader)
        {

            pContentMetaDataMaster.ContentKey = Convert.ToInt32(pDbDataReader["ContentKey"]);
            pContentMetaDataMaster.ContentCode = Convert.ToString(pDbDataReader["ContentCode"]);
            pContentMetaDataMaster.ContentName = Convert.ToString(pDbDataReader["ContentName"]);
            pContentMetaDataMaster.ContentMetaTagKeyword = Convert.ToString(pDbDataReader["ContentMetaTagKeyword"]);
            pContentMetaDataMaster.ContentMetaTagDescription = Convert.ToString(pDbDataReader["ContentMetaTagDescription"]);
            pContentMetaDataMaster.ContentHtmlData = Convert.ToString(pDbDataReader["ContentHtmlData"]);
            pContentMetaDataMaster.ContentTypeKey = Convert.ToInt32(pDbDataReader["ContentTypeKey"]);
            pContentMetaDataMaster.ContentTotalDownloads = Convert.ToInt32(pDbDataReader["ContentTotalDownloads"]);
            pContentMetaDataMaster.ContentTotalLikes = Convert.ToInt32(pDbDataReader["ContentTotalLikes"]);
            pContentMetaDataMaster.ContentTotalViews = Convert.ToInt32(pDbDataReader["ContentTotalViews"]);


            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentMetaDataMaster, pDbDataReader);

        }

        private void TranslateContentMetaDataMasterFromEntityToDatabaseParameters(DbCommand pDbCommand, ContentMetaDataMaster pContentMetaDataMaster)
        {
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentCode", ParameterDirection.Input, DbType.String, pContentMetaDataMaster.ContentCode);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentName", ParameterDirection.Input, DbType.String, pContentMetaDataMaster.ContentName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentMetaTagKeyword", ParameterDirection.Input, DbType.String, pContentMetaDataMaster.ContentMetaTagKeyword);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentMetaTagDescription", ParameterDirection.Input, DbType.String, pContentMetaDataMaster.ContentMetaTagDescription);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentHtmlData", ParameterDirection.Input, DbType.String, pContentMetaDataMaster.ContentHtmlData);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentTypeKey", ParameterDirection.Input, DbType.Int32, pContentMetaDataMaster.ContentTypeKey);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentTotalDownloads", ParameterDirection.Input, DbType.Int32, pContentMetaDataMaster.ContentTotalDownloads);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentTotalLikes", ParameterDirection.Input, DbType.Int32, pContentMetaDataMaster.ContentTotalLikes);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentTotalViews", ParameterDirection.Input, DbType.Int32, pContentMetaDataMaster.ContentTotalViews);
        }
        private void TranslateContentStatisticsFieldsFromReaderToEntity(ContentStatistics pContentStatistics, DbDataReader pDbDataReader)
        {

            pContentStatistics.ContentKey = Convert.ToInt32(pDbDataReader["ContentKey"]);
            pContentStatistics.ContentCode = Convert.ToString(pDbDataReader["ContentCode"]);
            pContentStatistics.ContentName = Convert.ToString(pDbDataReader["ContentName"]);
            pContentStatistics.ContentMetaTagKeyword = Convert.ToString(pDbDataReader["ContentMetaTagKeyword"]);
            pContentStatistics.ContentMetaTagDescription = Convert.ToString(pDbDataReader["ContentMetaTagDescription"]);
            pContentStatistics.ContentHtmlData = Convert.ToString(pDbDataReader["ContentHtmlData"]);
            pContentStatistics.ContentTypeKey = Convert.ToInt32(pDbDataReader["ContentTypeKey"]);
            pContentStatistics.ContentTypeCode = Convert.ToString(pDbDataReader["ContentTypeCode"]);
            pContentStatistics.ContentTypeName = Convert.ToString(pDbDataReader["ContentTypeName"]);
            pContentStatistics.ContentTotalDownloads = Convert.ToInt32(pDbDataReader["ContentTotalDownloads"]);
            pContentStatistics.ContentTotalLikes = Convert.ToInt32(pDbDataReader["ContentTotalLikes"]);
            pContentStatistics.ContentTotalViews = Convert.ToInt32(pDbDataReader["ContentTotalViews"]);
            pContentStatistics.Sys_DateOfCreation = Convert.ToDateTime(pDbDataReader["Sys_DateOfCreation"]);
            pContentStatistics.Sys_CreatedByID = Convert.ToString(pDbDataReader["Sys_CreatedByID"]);


            if (!pDbDataReader.IsDBNull(pDbDataReader.GetOrdinal("TotalVotes")))
            {
                pContentStatistics.TotalVotes = Convert.ToInt32(pDbDataReader["TotalVotes"]);
            }
            else
            {
                pContentStatistics.TotalVotes = 0;
            }

            if (!pDbDataReader.IsDBNull(pDbDataReader.GetOrdinal("AvrageRating")))
            {
                pContentStatistics.AvrageRating = Convert.ToInt32(pDbDataReader["AvrageRating"]);
            }
            else
            {
                pContentStatistics.AvrageRating = 0;
            }


            pContentStatistics.UserFirstName = Convert.ToString(pDbDataReader["UserFirstName"]);
            pContentStatistics.UserLastName = Convert.ToString(pDbDataReader["UserLastName"]);
            pContentStatistics.UserLoginID = Convert.ToString(pDbDataReader["UserLoginID"]);



        }
        #endregion Private Methods

    }
}
